package org.zy.validate;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

public class MyJdbcRealmTest {
	final static Logger log = LoggerFactory.getLogger(MyJdbcRealmTest.class);

	@Before
	public void setUp() throws Exception {
		System.out.println("添加环境..");
		Factory<SecurityManager> factory = new IniSecurityManagerFactory(
				"classpath:config/main.ini");
		SecurityManager sm = factory.getInstance();
		SecurityUtils.setSecurityManager(sm);
	}

	@Test
	public void test() {

		Subject user = SecurityUtils.getSubject();
		if (!user.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken("admin",
					"admin");
			token.setRememberMe(true);
			try {
				System.out.println("开始登陆");
				user.login(token);
				System.out.println("登陆成功");
			} catch (UnknownAccountException e) {
				log.info("账号未识别");
			} catch (IncorrectCredentialsException e) {
				log.info("不正确的凭据");
			} catch (LockedAccountException e) {
				log.info("账户被锁");
			} catch (AuthenticationException e) {
				log.info("其他错误");
			}
			  
		}
		
	}
	@Test
	public void test2(){
		Subject user = SecurityUtils.getSubject();
		try {
			log.info(JSON.toJSONString("asdasd"+user.getPrincipal()));
			if (user.hasRole("超级管理员")) {
				System.out.println("存在当前角色");
			}
			else
			{
				System.out.println("不存在");
			}
			user.checkPermission("print:超级管理员");
			user.checkPermission("dss:超级管理员");
		} catch (Exception e) {
			System.out.println("没有权限");
		}

		System.out.println(user.getSession());

		
	}
	@Test
	public void test3(){
		
		
		
	}
}
